<?php

namespace app\index\controller;

use app\common\model\User;
use think\Controller;
use app\commom\controller\Sms;
use app\common\controller\Com as Funcom;

//公共方法
class Com extends Funcom
{

    public function _empty($name){
        return $name.'  404';
    }

    //登录
    public function login(){

        if(request()->isAjax() || request()->isPost()) {
            $auth_code=input('code','','trim');
            if(!$auth_code)$this->error('请输入验证码');
            if(!captcha_check($auth_code))$this->error('验证码不正确');

            //验证输入信息
            $data = $this->request->post();
            $result = $this->validate($data,'app\admin\validate\AdminValidate.login');
            if(true !== $result)$this->error($result);
            $account = $data['account'];
            $password = $data['password'];

            $object= new User();
            $res=$object->login($account,$password);
            if(!$res){
                $this->error($object->getError());
            }

            return $this->success('登录成功','/user/index');
        }

        $uid = is_user_login();
        if($uid)return $this->redirect('/user/index');

        return $this->fetch();
    }

    //注册
    public function register(){
        if (request()->isPost()) {
            $account=input('account','','trim');
            $password=input('password','','trim');
            $repassword=input('repassword','','trim');
            if(!$password)$this->error('请输入密码');
            if(!checkPwd($password))$this->error('密码格式不正确');
            if($password!=$repassword)$this->error('确认密码不正确');

            //验证短信验证码
            $reg_time=time()-config('common.SMS_REG_TIME');
            $_where="phone='{$account}' AND type=1";
            $verify=db('sms_code')->where($_where)->find();
            if(!$verify)$this->error('短信验证码不存在');
            if($verify['time']<$reg_time)$this->error('短信验证码已过期');

            $data['mobile']=$data['account']=$account;
            $data['password']=md5($password);
            $data['time_reg']=time();
            $data['state']=1;
            $data['hide_flag']=0;
            $result = $this->validate($data ,'app\admin\validate\User.register');
            if($result !== true)$this->error($result);
            $uid=db('user')->insertGetId($data);
            if(!$uid)$this->error('注册失败');

            $object= new User();
            $res=$object->login($account,$password);
            if(!$res)$this->error($object->getError());
            return $this->success('注册成功','/user/index');
        }

        return $this->fetch();
    }


    //找回密码
    public function forget(){
        if(request()->isPost()){
            $account=input('account','','trim');
            $password=input('password','','trim');
            $repassword=input('repassword','','trim');
            if(!$password)$this->error('请输入密码');
            if(!checkPwd($password))$this->error('密码格式不正确');
            if($password!=$repassword)$this->error('确认密码不正确');

            //短信验证码
            $verifymsg=input('code','','trim');
            if(!$verifymsg)$this->error('请输入短信验证码');

            //验证短信验证码
            $reg_time=time()-config('common.SMS_REG_TIME');
            $_where="phone='{$account}' AND type=2";
            $verify=db('sms_code')->where($_where)->find();
            if(!$verify)$this->error('短信验证码不存在');
            if($verify['time']<$reg_time)$this->error('短信验证码已过期');
            if($verify['content']!=$verifymsg)$this->error('短信验证码不正确');

            $data['password']=md5($password);
            $_map['account']=$account;
            $uid=db('user')->where($_map)->update($data);
            if(false===$uid)$this->error('修改失败');

            $object= new User();
            $res=$object->login($account,$password);
            if(!$res)$this->error($object->getError());
            return $this->success('修改成功','/user/index');
        }

        return $this->fetch();
    }




    /**
     * 注册获取短信验证码
     * @author leiqianyong 2015-11-21 9:21 AM
     */
    public function getVerify(){
        $checkVerify = input('verify');
        if(!captcha_check($checkVerify))$this->error('验证码不正确!');

        $phone = input('phone');
        if(!$phone || !is_mobile($phone))$this->error('请正确填写手机号码!');

        //验证类型  1-注册  2-找回密码  3-重新绑定
        $param['type']=$type=input('type',1);
        $where['account']=$phone;
        $_info = db('user')->where($where)->find();
        if(1==$type && $_info)$this->error('该手机号码已被注册');
        if(2==$type && empty($_info))$this->error('该手机号码不存在');

        $param['mobile']=$phone;
        $object=new \app\common\controller\Sms();
        $res=$object->SmsSend($param);
        header('Content-Type:application/json; charset=utf-8');
        return json($res);
    }



    //验证码
    public function verifyImg(){
        $config =    [
            //验证码字体大小
            'fontSize'     =>    18,
            //验证码位数
            'length'       =>    4,
            'imageW' => 120,
            'imageH' => 35,
            //是否画混淆曲线
            'useCurve' => false,
            //关闭验证码杂点
            'useNoise'    =>    true,
            'codeSet' => '0123456789',
        ];
        return captcha('',$config);
    }

}